package com.bw.liuyushan.liuyushan20210929.MVP.Fragment;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bw.liuyushan.liuyushan20210929.Adapter.TabRVAdapter4;
import com.bw.liuyushan.liuyushan20210929.Entity.JsonBean;
import com.bw.liuyushan.liuyushan20210929.MVP.Activity.GoodCarActivity;
import com.bw.liuyushan.liuyushan20210929.MVP.Activity.SimItemActivity;
import com.bw.liuyushan.liuyushan20210929.MVP.Contrack.IContrackTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Model.BaseModelTest;
import com.bw.liuyushan.liuyushan20210929.MVP.Presenter.BasePresenterTest;
import com.bw.liuyushan.liuyushan20210929.R;
import com.bw.liuyushan.mvp_lib.View.BaseFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.TabView;

/**
 * Liuyushan20210929
 * file name is : HomeFragment
 * created by Ender on 2021/9/29 14:00.
 * author : 刘雨杉
 * Describe:
 */
public class ListFragment extends BaseFragment<BasePresenterTest> implements IContrackTest.C_ViewTest, OnRefreshLoadMoreListener {
    private EditText selectit;
    private VerticalTabLayout tabV;
    private SmartRefreshLayout smTabv;
    private RecyclerView rvTabv;
    private ImageView gocar;
    int page = 1;
    TabRVAdapter4 adapter3;
    boolean isAdapter;
    List<JsonBean.DataBean> data;


    @Override
    public int bindLayout() {
        return R.layout.fragment_liste;
    }

    @Override
    public void initView() {
        //防止内存泄露
        mPresenter = new BasePresenterTest(new BaseModelTest(), this);

        selectit = (EditText) findViewById(R.id.selectit);
        tabV = (VerticalTabLayout) findViewById(R.id.tabV);
        gocar = (ImageView) findViewById(R.id.gocar);

        smTabv = (SmartRefreshLayout) findViewById(R.id.sm_tabv);
        rvTabv = (RecyclerView) findViewById(R.id.rv_tabv);

        smTabv.setOnRefreshLoadMoreListener(this);
        //瀑布流
        rvTabv.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public void initDate() {
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
        gocar.setOnClickListener(new View.OnClickListener() {//点击跳转购物车
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), GoodCarActivity.class);
                startActivity(intent);
            }
        });
        ArrayList<String> strings1 = initArray();
        tabV.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return strings1.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public ITabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public ITabView.TabTitle getTitle(int position) {
                ITabView.TabTitle build = new ITabView.TabTitle.Builder()
                        .setContent(strings1.get(position))//标题的集合
                        .setTextColor(Color.BLACK, Color.GRAY)//字体颜色->选中颜色,未选中颜色
                        .build();
                return build;
            }

            @Override//背景 按需求设置 可忽略
            public int getBackground(int position) {
                return 0;
            }
        });
        //VerticalTabLayout监听事件 点击直接刷新 翻页改变数据
        tabV.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                switch (position) {
                    case 0:
                        page = 1;
                        smTabv.autoRefresh();
                        break;
                    case 1:
                        page = 2;
                        smTabv.autoRefresh();
                        break;
                    case 2:
                        page = 3;
                        smTabv.autoRefresh();
                        break;
                    case 3:
                        page = 4;
                        smTabv.autoRefresh();
                        break;
                    case 4:
                        page = 5;
                        smTabv.autoRefresh();
                        break;
                    case 5:
                        page = 6;
                        smTabv.autoRefresh();
                        break;
                    case 6:
                        page = 7;
                        smTabv.autoRefresh();
                        break;
                    case 7:
                        page = 8;
                        smTabv.autoRefresh();
                        break;
                    case 8:
                        page = 9;
                        smTabv.autoRefresh();
                        break;
                    case 9:
                        page = 10;
                        smTabv.autoRefresh();
                        break;
                    case 10:
                        page = 11;
                        smTabv.autoRefresh();
                        break;

                }
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });
        selectit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    @NotNull
    private ArrayList<String> initArray() {
        ArrayList<String> strings1 = new ArrayList<>();
        strings1.add("有货");
        strings1.add("上装");
        strings1.add("裤装");
        strings1.add("新品");
        strings1.add("潮流");
        strings1.add("潮流");
        strings1.add("潮流");
        strings1.add("潮流");
        strings1.add("潮流");
        strings1.add("潮流");
        strings1.add("潮流");
        return strings1;
    }

    @Override
    public void ShowDate(JsonBean jsonBean) {
        data = jsonBean.getData();
        initAdapter(data);
    }

    private void initAdapter(List<JsonBean.DataBean> data) {
        smTabv.finishLoadMore();
        smTabv.finishRefresh();
        if (adapter3 == null) {
            adapter3 = new TabRVAdapter4(data);
            rvTabv.setAdapter(adapter3);
            adapter3.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                    Intent intent = new Intent(getActivity(), SimItemActivity.class);
                    intent.putExtra("obj", data.get(position));
                    startActivity(intent);
                }
            });
        } else {
            if (isAdapter) {
                adapter3.getData().clear();
            }
            adapter3.getData().addAll(data);
            adapter3.notifyDataSetChanged();
        }
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        isAdapter = false;
//        page++;
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        isAdapter = true;
//        page = 1;
        mPresenter.mpDate("ios/cf/dish_list.php?stage_id=1&limit=20&page=" + page);
    }
}
